package com.usc.uscface.controller;

import com.usc.uscface.enums.*;
import com.usc.uscface.tools.Face;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.usc.uscface.controller
 * @date 2020/11/18 3:04
 */
@Controller
public class FaceController {

    Face face = new Face("http://127.0.0.1","5000");

    @Value("${vector_target}")
    private String vector_target;
    @Value("${picture_target}")
    private String picture_target;
    @Value("${tempPath}")
    private String tempPath;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    /**
     * 文件入库并进行编码
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ResponseBody
    public CodeResponse upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
        List<MultipartFile> files = params.getFiles("file");//file为文件项的name值
        String banjiName = "";
        for (int j = 0; j < files.size(); j++) {
            banjiName = face.uploadFile(files.get(j), picture_target);
        }
        picture_target = picture_target+"\\"+banjiName;
        //对库里面的文件进行编码
        String vector_targets = vector_target ;
        CodeResponse json = face.FileCode(picture_target, vector_targets);
        return json;
    }

    /**
     * 对单个文件进行切割
     * @return
     */
    @PostMapping("/crop")
    @ResponseBody
    public CropResponses FileCrop(MultipartFile file3) throws IOException {
        CropResponses fileCode = face.crop(file3,tempPath);
        return fileCode;
    }


    /**
     * 人脸比对
     * @param file4
     * @return
     * @throws IOException
     */
    @PostMapping("/compare")
    @ResponseBody
    public Compare Compare(MultipartFile file4) throws IOException {
        Compare json = face.compare(file4, tempPath, vector_target);
        return json;
    }



}
